<?php

function _tranxuantuan_add() {
	$form_add = drupal_get_form('_tranxuantuan_form_add_form');
	return drupal_render($form_add);
}

function _tranxuantuan_form_add_form($form, &$form_state) {
	$form['ma_de'] = array (
		'#type' => 'textfield',
		'#size' => 10,
		'#title' => t('Mã đề'),
	);
$form_state['storage']['info'] = isset($form_state['storage']['info']) ? $form_state['storage']['info'] : 0;
	$form['info'] = array (
	'#type' => 'fieldset',
	'#title' => t('Đáp án'),
	'#tree' => TRUE,
	'#prefix' => '<div id="info">',
	'#suffix' => '</div>',
	);
		$form['info'][0] = array (
		'#type' => 'fieldset',
		'#title' => t('Câu 1'),
		'#tree' => TRUE,
		'#prefix' => '',
		'#suffix' => '',
		);
		$form['info'][0]['dap_an'] = array (
		 '#type' => 'select',
		 '#title' => t('Đáp án'),
		 '#options' => array(
			'a' => t('A'),
			'b' => t('B'),
			'c' => t('C'),
			'd' => t('D'),
		 ),
		);
		$form['info'][0]['thang_diem'] = array (
			'#type' => 'textfield',
			'#size' => 10,
			'#title' => t('Điểm'),
		);
		
	if($form_state['storage']['info']) {
		for ($i = 1; $i<= $form_state['storage']['info']; $i++) {
			$j=$i+1;
			$form['info'][$i] = array (
			'#type' => 'fieldset',
			'#title' => t('Câu '.$j),
			'#tree' => TRUE,
			'#prefix' => '',
			'#suffix' => '',
			);
				$form['info'][$i]['dap_an'] = array (
				 '#type' => 'select',
				 '#title' => t('Đáp án'),
				 '#options' => array(
					'a' => t('A'),
					'b' => t('B'),
					'c' => t('C'),
					'd' => t('D'),
				 ),
				);
				$form['info'][$i]['thang_diem'] = array (
					'#type' => 'textfield',
					'#size' => 10,
					'#title' => t('Điểm'),
				);		
		}
	}	
	$form_state['storage']['info']++;
	$form['add_info'] = array(
    '#type' => 'button',
    '#value' => t('Thêm câu hỏi'),
    '#href' => '',
    '#ajax' => array(
    'callback' => 'custom_registration_ajax_add_participant',
    'wrapper' => 'info',
   ),
	 );
	$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Lưu đáp án'),			
  );
	return $form;
}
 function custom_registration_ajax_add_participant($form, $form_state) {
  return $form['info'];
}
function _tranxuantuan_form_add_form_validate($form, &$form_state) {
}
function _tranxuantuan_form_add_form_submit($form, &$form_state) {
	//dpm($form_state);
	$module_path = drupal_get_path('module', 'tranxuantuan');
	$dapan = '';
	$diem = '';
	$json_data = array();
	$made = $form_state['values']['ma_de'];
	foreach ($form_state['values']['info'] as $key=>$info) {
		dpm($key);
		$dapan .= $info['dap_an'];
		$diem .= $info['thang_diem'];
		array_push($json_data,array(
			'dap_an' => $info['dap_an'],
			'thang_diem' => $info['thang_diem']
			)
		);
	}
	dpm($json_data);
	print_r ($json_data);
	$data = json_encode($json_data);
	$data1 = json_decode($data);
	dpm($data1);
	file_put_contents($module_path .'/da/'.$made .'.json', serialize($json_data));
	//dpm($dapan);
	//dpm($diem);
	// node save
/**	$node = new stdClass(); // creat a new node object
	$node->type = "dap_an";
	$node->title = $made;
	$node->language = 'en';
	node_object_prepare($node);
	$node->field_dap_an['und'][0]['value'] = $dapan;
	$node->field_thang_diem['und'][0]['value'] = $diem;
	$node = node_submit($node);
	node_save($node); */
}